<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 星云微讯科技有限公司
 * 手机端页面
 *  wap/index.php
 *  lichaoliang  2015-03-13
 */
class Index extends CI_Controller
{

    private $user_openid = '';

    public function __construct()
    {
        parent::__construct();
        $this->load->model('comm_model');
        $this->load->model('hot_model');
        $this->load->model('recommend_model');
        $this->load->model('prize_model');
        $this->load->model('user_model');
        $this->load->model('visitlog_model');
        $this->user_openid = $this->user_model->get_user_info();
        date_default_timezone_set('Asia/Shanghai');
        header("Content-type:text/html;charset=utf-8");
    }

    public function index()
    {
        $this->load->helper('date');

        // 获取城市

        $ip = $this->input->ip_address();

        if ($ip == "127.0.0.1") {
            $ip = '113.88.13.232';
        }

        $type = !empty($_GET['type']) ? intval($_GET['type']) : 0;

//        $content = file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='.$ip);
//        $arr = explode('=',$content);
//        $arr[1] = substr($arr[1], 0, -1);
//        $array =  json_decode($arr[1]);
//        if(empty($array->city)){
//            $array->city = '深圳';
//        }
//        $cityid=$this->comm_model->city_one($array->city);
//        if($cityid){
//            $cityid = $cityid['id'];
//        } else {
        $cityid = 77;
        //}
        $hot = $this->hot_model->get_list(0, 10, array());  // 热门游戏
        $prize = $this->hot_model->get_list(0, 10, array(), true); // 最新游戏
        $history = $this->visitlog_model->get_history($this->user_openid, 20); //玩家最近的20条记录

        //访问记录，为用户推荐4个游戏
        $exclude_ids = array();
        foreach ($history as $game){
            $exclude_ids[] = $game['gameid'];
        }
        $history_recommend = $this->hot_model->get_recommend(4,$exclude_ids); //历史记录页面推荐游戏

        $this->load->library('Mobile_Detect');  // 判断手机操作系统
        $phone = 1;
        if ($this->mobile_detect->isWindowsMobileOS()) {
            $phone = 3;
        } elseif ($this->mobile_detect->isWindowsPhoneOS()) {
            $phone = 3;
        } elseif ($this->mobile_detect->isAndroidOS()) {
            $phone = 1;
        } elseif ($this->mobile_detect->isiPhone()) {
            $phone = 2;
        } elseif ($this->mobile_detect->isiPad()) {
            $phone = 2;
        }

        // 判断是否微信端
        $guide = 0;
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            $guide = 0;
        } else {
            $guide = 1;
        }

        // 广告数据源
        $advert = $this->comm_model->get_list(0, 100, 'advert', 'id desc');

        //加载adsense广告代码
        $this->config->load('ads');
        $adsense = $this->config->item('adsense');

        // 城市
        $city = $this->comm_model->has_game_city();
        $data = array(
            'hot' => $hot,
            'prize' => $prize,
            'recommend' => $history,
            'history_recommend' => $history_recommend,
            'phone' => $phone,
            'advert' => $advert,
            'city' => $city,
            'cityid' => $cityid,
            'type' => $type,
            'guide' => $guide,
            'adsense' => $adsense);

        $this->load->view('wap/index.php', $data);
    }


    // prize_view 有奖游戏详情
    public function prize_view($id)
    {
        $view = $this->prize_model->get_one($id);
        $array_category = $this->prize_model->category_list($id, $view['category_id']);

        //  点击量+1
        $this->comm_model->update($id, array('click' => $view['click'] + 1), 'game_prize');
        $data = array('view' => $view, 'array_category' => $array_category);
        $this->load->view('wap/page.php', $data);

    }

    public function recommend_view($id)
    {
        $this->load->library('Mobile_Detect');  // 判断手机操作系统
        $phone = 1;
        if ($this->mobile_detect->isWindowsMobileOS()) {
            $phone = 3;
        } elseif ($this->mobile_detect->isWindowsPhoneOS()) {
            $phone = 3;
        } elseif ($this->mobile_detect->isAndroidOS()) {
            $phone = 1;
        } elseif ($this->mobile_detect->isiPhone()) {
            $phone = 2;
        } elseif ($this->mobile_detect->isiPad()) {
            $phone = 2;
        }

        // 判断是否微信端
        $guide = 0;
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            $guide = 0;
        } else {
            $guide = 1;
        }

        $view = $this->recommend_model->get_one($id);
        $array_category = $this->recommend_model->category_list($id, $view['category_id'], $view['type']);
        $this->comm_model->update($id, array('click' => 'click+1'), 'recommend');
        $view_img = $this->recommend_model->images_list($view['id']);
        $data = array('view' => $view, 'array_category' => $array_category, 'view_img' => $view_img, 'guide' => $guide, 'phone' => $phone);
        $this->load->view('wap/page-down.php', $data);
    }

    //异步统计游戏点击次数，已在跳转页面统计，下个版本移除该处理
    public function click_ajax()
    {
        $id = !empty($_POST['id']) ? intval($_POST['id']) : 0;
        $row = $this->comm_model->get_one($id, 'game_hot');
        $rs = $this->comm_model->update($id, array('click' => $row['click'] + 1), 'game_hot');
        if ($rs) {
            echo 1;
            exit;
        } else {
            echo 0;
            exit;
        }
    }

    /**
     * 游戏链接获取及获取原始链接跳转
     * @todo:把游戏页面嵌入到视图框架，那么在该视图框架上就可以灵活附加一些业务
     * @todo:比如转发文案、通过转发收集用户关系链、全局浮动功能按钮
     * @todo:根据游戏类型参数到相应的数据表读取游戏数据并跳转
     * @param int $id 游戏id
     * @param string $type 游戏类型（不同表）
     */
    public function togame($id, $type = 'hot')
    {
        $this->load->helper('url');
        $this->config->load('channels');//渠道值可配置

        $channels = $this->config->item('channels');
        $channel = empty(trim($_GET['channel'])) ? trim($_GET['from']) : trim($_GET['channel']);
        $channel = !empty($channel) && isset($channels[$channel]) ? $channel : 'list';//过滤非法渠道值
        $id = !empty($id) ? intval($id) : 0;

        $row = $this->comm_model->get_one($id, 'game_hot');
        $this->comm_model->update($id, array('click' => $row['click'] + 1), 'game_hot');//统计点击次数

        if ($row && strpos($row['url'], 'http') !== false) {
            $arr_row['gameid'] = $id;
            $arr_row['picture'] = $row['picture'];
            $arr_row['title'] = $row['title'];
            $this->visitlog_model->add_log($this->user_openid, $arr_row, $channel); //缓存用户访问历史记录
            redirect($row['url']);

            //$this->load->view('wap/bridge', array('game'=>$row)); todo:游戏页面嵌入到视图框架
        } else {
            redirect(base_url("index.php/wap/index"));
        }
    }
}